<template>
  <div class="hello">
    确定要将该学生数据新增为溯源数据吗？ 学生：{{ this.studentName }},身份证：{{
      this.idCard
    }}
    新增溯源数据后，该学生信息将增加到数据中心 请补充以下溯源信息
    <el-form
      ref="ruleForm"
      :model="ruleForm"
      :rules="rules"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="入学口" prop="entrance">
        <el-select v-model="ruleForm.entrance" placeholder="请选择活动区域">
          <el-option label="统招" value="统招" />
          <el-option label="单招" value="单招" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否在籍" prop="if_registered">
        <el-switch v-model="ruleForm.if_registered" />
      </el-form-item>
      <el-form-item label="注册学籍号" prop="student_registration_number">
        <el-input v-model="ruleForm.student_registration_number" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >确认</el-button
        >
        <el-button @click="resetForm('ruleForm')">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'HelloWorld',
  props: {
    studentName: {},
    idCard: {}
  },
  data () {
    return {
      ruleForm: {
        student_registration_number: '',
        entrance: '',
        if_registered: false
      },
      rules: {
        student_registration_number: [
          { required: true, message: '请输入学籍号', trigger: 'blur' },
          // { min: 8, max: 8, message: '长度在 8个字符，具体多少个字符不晓得你们去确定', trigger: 'blur' }
        ],
        entrance: [
          { required: true, message: '请选择入学口', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        this.ruleForm.name = this.studentName
        this.ruleForm.idCard = this.idCard
        this.ruleForm.bornDate = this.getBirthdayFromIdCard(this.idCard)
        var formData = JSON.stringify(this.ruleForm)
        if (valid) {
          this.$post('dataCenter/insertStudentSource', {
            formData
          }).then((r) => {
            this.$emit('showDialog', false)
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    getBirthdayFromIdCard: function (idCard) {
      var birthday = ''
      if (idCard) {
        if (idCard !== null && idCard !== '') {
          if (idCard.length === 15) {
            birthday = '19' + idCard.substr(6, 6)
          } else if (idCard.length === 18) {
            birthday = idCard.substr(6, 8)
          }
          birthday = birthday.replace(/(.{4})(.{2})/, '$1-$2-')
        }
      }

      return birthday
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style >
</style>
